home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Portable Patmos 1.1 / patmos-src / src / pipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-19  |  923 b   |  44 lines  |  [TEXT/KAHL]

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include "crtlocal.h"
  4.  
  5. typedef struct
  6.     {
  7.     long    readptr,writeptr;
  8.     char    buffer[8192];
  9.     } pipe_struct;
  10.  
  11. long readpipe(int fd, char *readbuffer, long size)
  12.     {
  13.     long actual = 0;
  14.     pipe_struct *buf = (pipe_struct *)crt_fd_tab[fd].fd;
  15.     while (size-- && (buf->writeptr != buf->readptr))
  16.         {
  17.         *readbuffer++ = buf->buffer[buf->readptr];
  18.         buf->readptr = (buf->readptr+1)&8191;
  19.         ++actual;
  20.         }
  21.     return actual;
  22.     }
  23.  
  24. long writepipe(int fd, char *writebuffer, long size)
  25.     {
  26.     long actual = 0;
  27.     pipe_struct *buf = (pipe_struct *)crt_fd_tab[fd].fd;
  28.     while (size-- && (buf->writeptr != (buf->readptr-1)&8191))
  29.         {
  30.         buf->buffer[buf->writeptr] = *writebuffer++;
  31.         buf->writeptr = (buf->writeptr+1)&8191;
  32.         ++actual;
  33.         }
  34.     return actual;
  35.     }
  36.  
  37. int pipe(int *fd)
  38.     {
  39.     fd[0] = fd[1] = next_fd(3);
  40.     crt_fd_tab[*fd].fd = (long)xmalloc(sizeof(pipe_struct));
  41.     crt_fd_tab[*fd].flags = O_PIPE;
  42.     return 0;
  43.     }
  44.